import { ajaxGet, jsonPost } from '@/util'
export default {
  state: {
    province: [],
    city: [],
    area: [],
    delayProvince: [],
    delayCity: [],
    delayArea: [],
    tree: [],
    cascaderList: [],
    cascaderListCity: [],
    departmentTree: []
  },
  mutations: {
    setCascaderList(state, data) {
      let loop = (arr) => {
        return arr.map(e => {
          e.value = e.id
          e.label = e.name
          e.title = e.name
          if (e.children && e.children.length) {
            e.children = loop(e.children)
          }
          return e
        })
      }
      state.cascaderList = loop(data)
    },
    setCascaderListCity(state, data) {
      let loop = (arr) => {
        return arr.map(e => {
          e.value = e.id
          e.label = e.name
          e.title = e.name
          if (e.children && e.children.length) {
            e.children = e.treeLevel == 1 ? loop(e.children) : []
          }
          return e
        })
      }
      state.cascaderListCity = loop(data)
    },
    setAddressTree(state, params) {
      let checked = params.checkedIdList
      let loop = (arr) => {
        return arr.map(e => {
          if (Array.isArray(checked) && checked.find(c => c === e.id)) {
            e.checked = true
          }
          e.value = e.id
          e.label = e.name
          e.title = e.name
          if (e.children && e.children.length) {
            e.children = loop(e.children)
          }
          return e
        })
      }
      state.tree = loop(params.data)
    },
    setAddress(state, object) {
      if(object.param.type == 'area' &&  !object.param.value) {
        return 
      }
      state[object.param.type] = object.data.map(item=> {
        return {
          id: item.id,
          value: item.name
        }
      })
      state[object.param.type].unshift({
        value: '请选择',
        id: 0
      })
    },
    setCityEmpty(state) {
      state.city = []
    },
    setAreaEmpty(state) {
      state.area = []
    },
    setDelayCityEmpty(state) {
      state.delayCity = []
    },
    setDelayAreaEmpty(state) {
      state.delayArea = []
    },
    setDepartmentTree(state, data) {
      state.departmentTree = data
    }
  },
  actions: {
    async  ajaxAddress({ commit, dispatch, state }, param) {
      return new Promise((resolve, reject) =>{
        ajaxGet('/tiger/area/list_by_parentId', param && { parentId: param.value }).then(data => {
          if (param && param.type) {
            commit('setAddress',{data: data,param: param})
            resolve(state[param.type][0].id)
          } else {
            resolve(data)
          }
        }).catch(data => {
          reject(data)
        })
      })
    },
    async ajaxAddressTree({ commit, dispatch, state }, checkedIdList) {
      return new Promise((resolve, reject) =>{
        let checked = checkedIdList
        ajaxGet('/tiger/area/find_tree').then((data) => {
          let params = {
            data: data,
            checkedIdList: checked
          }
          commit('setAddressTree',params)
        }).catch(data => {
          reject(data)
        })
      })
    },
    async ajaxCascaderList({ commit, dispatch, state }) {
      if (!state.cascaderList.length) {
        return new Promise((resolve, reject) =>{
          ajaxGet('/tiger/area/find_tree').then((data) => {
            commit('setCascaderList',data)
          }).catch(data => {
            reject(data)
          })
        })
      } else {
        return state.cascaderList
      }
    },
    async ajaxCascaderListCity({ commit, dispatch, state }) {
      if (!state.cascaderListCity.length) {
        return new Promise((resolve, reject) =>{
          ajaxGet('/tiger/area/find_tree').then((data) => {
            commit('setCascaderListCity',data)
          }).catch(data => {
            reject(data)
          })
        })
      } else {
        return state.cascaderListCity
      } 
    },
    async getDepartmentTree({ commit, dispatch, state }) {
      jsonPost('department/listDepartmentTree').then(res => {
        const func = (arr) => {
            arr.forEach(item => {
                item.label = item.name
                item.value = item.id
                if (item.children && item.children.length) {
                func(item.children)
                }
            })
        }
        func(res)
        commit('setDepartmentTree', res)
      })
    }
  },
  getters:{
    addressList:(state) => state.province,
    cityList:(state) => state.city,
    areaListQ:(state) => state.area,
    delayAddressList:(state) => state.delayProvince,
    delayCityList:(state) => state.delayCity,
    delayAreaListQ:(state) => state.delayArea,
    tree:(state) => state.tree,
    cascaderList: (state) => state.cascaderList,
    cascaderListCity: (state) => state.cascaderListCity,
    departmentTree: (state) => state.departmentTree
  }
}